from querier.esquerier import ElasticSearchQuerier


WORDCLOUD_TOP_N = 100


class WechatTagsQuerier(ElasticSearchQuerier):

    def __init__(self, es, index, doc_type):
        super(WechatTagsQuerier, self).__init__(None, None, None)
        self.es = es
        self.index = index
        self.doc_type = doc_type

    def search(self, args):
        id_ = args['biz_code']
        res = self.es.get(index=self.index, doc_type=self.doc_type, id=id_)
        if res['found']:
            keywords = res['_source']['keywords']
            weight = res['_source']['keywords_weight']
            len_keywords = len(keywords)
            len_weight = len(weight)
            length = len_keywords if len_keywords <= len_weight else len_weight
            ret = [{"text": keywords[i], "weight": weight[i]} for i in range(0, length)]

            return {'tags': ret}
        return {'message': 'biz_code=%s not found.' % id_}

    def _build_query(self, args):
        pass

    def _build_result(self, es_result, param):
        pass
